package shoppingmall.manage.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import shoppingmall.manage.dto.EditCommodityOrderStateParamsVO;
import shoppingmall.manage.pojo.CommodityOrder;
import shoppingmall.manage.dto.PageBean;
import shoppingmall.manage.dto.Result;
import shoppingmall.manage.service.CommodityOrderService;

import java.util.Map;
import java.util.UUID;

/**
 * 订单管理相关 controller层
 */
@Slf4j
@RestController
public class CommodityOrderController {

    @Autowired
    private CommodityOrderService commodityOrderService;

    /**
     * 分页查询所有的平台订单信息
     */
    @GetMapping("/manage/order")
    public Result getCommodityOrderList (@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int pageSize) {
        log.info("商品订单查询page:{},pageSize:{}",page,pageSize);
        //调用服务层方法
        PageBean commodityOrderList = commodityOrderService.getCommodityOrderList(page, pageSize);
        //判断pageBean对象
        if(commodityOrderList.getTotal() == 0) {
            return Result.error("未查询到订单数据!");
        }
        //返回结果
        return Result.success(commodityOrderList);
    }

    /**
     * 修改订单信息(通过字符串订单id)
     */
    @PutMapping("/manage/order")
    public Result updateCommodityOrder (@RequestBody CommodityOrder commodityOrder){
        log.info("修改订单信息,订单id={}",commodityOrder.getOrderId());
        //调用服务层方法
        boolean isUpdate = commodityOrderService.updateCommodityOrder(commodityOrder);
        //判断是否修改成功
        if(!isUpdate){
            return Result.error("订单状态更新失败!");
        }
        return Result.success("订单状态更新成功!");
    }
    /**
     * 删除订单信息(通过字符串订单id)
     */
    @DeleteMapping("/manage/order")
    public Result deleteCommodityOrder (@RequestParam String orderId) {
        log.info("删除订单信息,订单id={}",orderId);
        //调用服务层方法
        boolean isUpdate = commodityOrderService.deleteCommodityOrder(orderId);
        //判断是否修改成功
        if(!isUpdate){
            return Result.error("订单删除失败!");
        }
        return Result.success("订单删除成功!");
    }

    /**************************复杂业务***********************************************/

    /**
     * 批量修改订单信息(通过字符串订单id  将已支付的订单状态---->已发货的订单状态)
     */
    @PutMapping("/manage/orders")
    public Result updateOrdersStateByOrderIds (@RequestBody EditCommodityOrderStateParamsVO editCommodityOrderStateParamsVO){
        log.info("批量修改订单信息,订单ids={},修改字段=订单状态,字段值={}",editCommodityOrderStateParamsVO.getOrderIds().toString(),editCommodityOrderStateParamsVO.getOrderState());
        //调用服务层方法
        boolean isAllUpdate = commodityOrderService.updateOrderStateByOrderIds(editCommodityOrderStateParamsVO.getOrderIds(), editCommodityOrderStateParamsVO.getOrderState());
        //判断是否修改成功
        if(!isAllUpdate){
            return Result.error("批量更新订单状态失败!");
        }
        return Result.success("批量更新订单状态成功!");
    }

    /**
     * 批量删除订单(通过字符串订单id 数组集合，批量删除订单)
     */
    @DeleteMapping("/manage/orders")
    public Result deleteOrdersByOrderIds (@RequestBody EditCommodityOrderStateParamsVO editCommodityOrderStateParamsVO) {
        log.info("批量删除订单信息,订单ids={}",editCommodityOrderStateParamsVO.getOrderIds().toString());
        //调用服务层方法
        boolean isAllDel = commodityOrderService.deleteOrdersByOrderIds(editCommodityOrderStateParamsVO.getOrderIds());
        //判断是否修改成功
        if(!isAllDel){
            return Result.error("批量删除订单失败!");
        }
        return Result.success("批量删除订单成功!");
    }

    /**
     * 按照选择的开始日期到结束日期筛选订单 startDate:"" endDate:""
     * 本接口开始采用@GetMapping的方式携带请求体参数，postman也已测试通过，但前端采用axios的get请求携带请求体参数时，后端报错，问了ai可能是axios的限制，无奈本接口采用post请求
     */
    @PostMapping("/manage/orders")
    public Result getCommodityOrderListByDate (@RequestBody EditCommodityOrderStateParamsVO editCommodityOrderStateParamsVO) {
        log.info("根据起始日期筛选订单列表,startDate={},endDate={}",editCommodityOrderStateParamsVO.getStartDate(),editCommodityOrderStateParamsVO.getEndDate());
        //调用服务层方法
        PageBean commodityOrderListByDate = commodityOrderService.getCommodityOrderListByDate(editCommodityOrderStateParamsVO.getStartDate(), editCommodityOrderStateParamsVO.getEndDate());
        //进行判断
        if(commodityOrderListByDate.getTotal() == 0) {
            return Result.success("未查询到商品订单数据!");
        }
        return Result.success(commodityOrderListByDate);
    }
    /**
     * 按照订单状态筛选订单(未支付，已支付，待发货，已发货，运输中，已收货，已评价，已取消，已退货)
     */
    @GetMapping("/manage/orders/state")
    public Result getCommodityOrderListByState (@RequestParam String orderState) {
        log.info("根据订单状态获取订单信息,订单状态={}",orderState);
        System.out.println(orderState);
        //调用服务层方法
        PageBean pageBean = commodityOrderService.getCommodityOrderListByState(orderState);
        //进行判断
        if(pageBean.getTotal() == 0) {
            return Result.success("未查询到订单数据!");
        }
        //响应结果
        return Result.success(pageBean);
    }

    /**
     * 订单排序接口(根据下单时间升降序、订单更新时间升降序、订单总价升降序 进行排序)
     * 字段 create_time desc asc(升序)
     * 字段 update_time desc asc
     * 字段 order_total_price desc asc
     */
    @GetMapping("/manage/orders/sort")
    public Result getCommodityOrderListSort (@RequestParam String fieldName, @RequestParam String sortRule) {
        log.info("根据字段排序订单列表,排序字段={},排序规则={}",fieldName,sortRule);
        //调用服务层方法
        PageBean pageBean = commodityOrderService.getCommodityOrderListSort(fieldName, sortRule);
        //进行判断
        if(pageBean.getTotal() == 0) {
            return Result.success("未查询到订单数据!");
        }
        //响应结果
        return Result.success(pageBean);
    }

    /**
     * 订单模糊搜索(根据订单号 或者 下单用户 进行模糊匹配)
     * 本接口开始采用@GetMapping的方式携带请求体参数，postman也已测试通过，但前端采用axios的get请求携带请求体参数时，后端报错，问了ai可能是axios的限制，无奈本接口采用post请求
     */
    @PostMapping("/manage/orders/like")
    public Result getCommodityOrderListByUserNameOrOrderId (@RequestBody Map<String, String> mapParams) {
        log.info("模糊搜索信息,搜索关键字,userName={},orderId={}",mapParams.get("userName"),mapParams.get("orderId"));
        //调用服务层方法
        PageBean pageBean = commodityOrderService.getCommodityOrderListByUserNameOrOrderId(mapParams);
        //进行判断
        if(pageBean.getTotal() == 0) {
            return Result.success("未查询到订单数据!");
        }
        //响应结果
        return Result.success(pageBean);
    }

    /******************************************************************************************/

    /**
     * 创建一条订单信息(前台api接口应该做的)
     * @@仅供测试使用
     */
    @PostMapping("/manage/order")
    public Result saveCommodityOrder (@RequestBody CommodityOrder commodityOrder) {
        log.info("创建一条订单信息,商品id={},下单用户id={}",commodityOrder.getCommodityId(),commodityOrder.getUserId());
        //临时生成订单号
        String orderId = UUID.randomUUID().toString().replaceAll("-", "");
        commodityOrder.setOrderId(orderId);
        //调用服务层方法
        boolean isSave = commodityOrderService.saveCommodityOrder(commodityOrder);

        if(!isSave) {
            return Result.error("订单创建失败!");
        }
        //返回结果
        return Result.success("订单创建成功!");
    }
}
